package com.example.lenovo.findmyclass;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class LoginActivity extends Activity  {
    private Button register_button;
    private Button login_button;
    private CheckBox rememberpsw;
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    private AutoCompleteTextView acTextView;
    private EditText et_user_id,et_psw;
    private String userId,psw,spPsw;
    private String[] str = {"山东建筑大学","山东大学","山东财经大学","青岛科技大学","青岛大学"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        acTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_expandable_list_item_1, str);
        acTextView.setAdapter(adapter);
        //et_user_name=findViewById(R.id.et_user_name);
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        rememberpsw  = (CheckBox)findViewById(R.id.remember_psw);
        et_user_id=findViewById(R.id.et_user_id);
        et_psw=findViewById(R.id.et_psw);
        boolean isRemember = pref.getBoolean("remember_password",true);
        if (isRemember){
            String userId = pref.getString("account","");
            String psw = pref.getString("password","");
            et_user_id.setText(userId);
            et_psw.setText(psw);
            rememberpsw.setChecked(true);
        }
        register_button = (Button) findViewById(R.id.register);
        login_button = (Button) findViewById(R.id.login);
        register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent register_intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(register_intent);
            }
        });
        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userId=et_user_id.getText().toString().trim();
                psw=et_psw.getText().toString().trim();
                //对当前用户输入的密码进行MD5加密再进行比对判断, MD5Utils.md5( ); psw 进行加密判断是否一致
                String md5Psw= MD5Utils.md5(psw);
                // md5Psw ; spPsw 为 根据从SharedPreferences中用户名读取密码
                // 定义方法 readPsw为了读取用户名，得到密码
                spPsw=readPsw(userId);
                if (TextUtils.isEmpty(userId)) {
                    Toast.makeText(LoginActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(psw)) {
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    // md5Psw.equals(); 判断，输入的密码加密后，是否与保存在SharedPreferences中一致
                } else if (md5Psw.equals(spPsw)) {
                    editor = pref.edit();
                    if (rememberpsw.isChecked()){
                        editor.putBoolean("remember_password",true);
                        editor.putString("account",userId);
                        editor.putString("password",psw);
                    }else {
                        editor.clear();
                    }
                    editor.apply();
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    saveLoginStatus(true, userId);
                    Intent login_intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(login_intent);
                    finish();
                }else if((spPsw!=null&&!TextUtils.isEmpty(spPsw)&&!md5Psw.equals(spPsw))){
                    Toast.makeText(LoginActivity.this, "输入的用户名和密码不一致", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(LoginActivity.this, "此用户名不存在", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    /**
     *从SharedPreferences中根据用户名读取密码
     */
    private String readPsw(String userId){
        //getSharedPreferences("loginInfo",MODE_PRIVATE);
        //"loginInfo",mode_private; MODE_PRIVATE表示可以继续写入
        pref=getSharedPreferences("loginInfo", MODE_PRIVATE);
        //sp.getString() userName, "";
        return pref.getString(userId , "");
    }
    /**
     *保存登录状态和登录用户名到SharedPreferences中
     */
    private void saveLoginStatus(boolean status,String userName){
        //saveLoginStatus(true, userName);
        //loginInfo表示文件名  SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        pref = getSharedPreferences("loginInfo", MODE_PRIVATE);
        //获取编辑器
        editor = pref.edit();
        //存入boolean类型的登录状态
        editor.putBoolean("isLogin", status);
        //存入登录状态时的用户名
        editor.putString("loginUserName", userName);
        //提交修改
        editor.commit();
    }

    /**显示数据， onActivityResult
     *startActivityForResult(intent, 1); 从注册界面中获取数据
     *int requestCode , int resultCode , Intent data
     *LoginActivity -> startActivityForResult -> onActivityResult();
    */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //super.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null){
            //是获取注册界面回传过来的用户名
            // getExtra().getString("***");
            String userId =data.getStringExtra("userId");
            if(!TextUtils.isEmpty(userId)){
                //设置用户名到 et_user_name 控件
                et_user_id.setText(userId);
                //et_user_name控件的setSelection()方法来设置光标位置
                et_user_id.setSelection(userId.length());
            }
        }
    }
}